package com.hyts.bmsp.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.JFinal;
import com.jfinal.log.Log;

/**
 * @ClassName: EncodeInterceptor
 * @Description: Encode 编码信息构建类型拦截器
 * @author: 李博/Alex
 * @date: 2017-1-9 下午09:00:30
 */
public class ExceptionInterceptor implements Interceptor {

	/**
	 * @fieldName: ENCODE_CHARSET
	 * @fieldType: String
	 * @Description: utf-8编码格式
	 */
	  private static final Log logger = Log.getLog(ExceptionInterceptor.class);
	
	/* (non Javadoc)
	 * @Title: intercept
	 * @Description: 拦截器进行设置编码类型数据
	 * @param inv
	 * @see com.jfinal.aop.Interceptor#intercept(com.jfinal.aop.Invocation)
	 */
	@Override
	 public void intercept(Invocation inv)
	  {
	    try
	    {
	      inv.invoke();
	    } catch (Exception e) {
	      logWrite(inv, e);
	    }
	  }

	private void logWrite(Invocation inv, Exception e)
	  {
	    StringBuilder sb = new StringBuilder("\n---------系统异常日志开始---------\n");
	    if ((inv != null) && (e != null)) {
	      if (JFinal.me().getConstants().getDevMode())
	        e.printStackTrace();
	      sb.append("控制器（Controller）:").append("inv.getController().getClass().getName()\n");
	      sb.append("方    法（Method）:").append(inv.getMethodName()).append("\n");
	      sb.append("异常类型（Exception Type）:").append(e.getClass().getName()).append("\n");
	    }
	    else {
	      sb.append("系统操作：判断的invocation调用对象为空或者Exception e对象为空 ---> 系统判断出现异常!");
	    }
	    sb.append("\n---------系统异常日志结束---------\n");
	    logger.error(sb.toString(), e);
	  }

}
